var baseURL = ''
// 	根据环境变量改变请求接口统一前缀
if(process.env.NODE_ENV === 'development'){
	baseURL = 'http://10.1.3.22:9999/zlhj_core'	//	开发环境
}else{
	baseURL = 'https://sit.linkfin-tech.com/'	//	线上环境
}

// // const cookie =  uni.setStorageSync('cookie', 'hello');
/** 
 * post方法，对应post请求 
 * @param {String} url [请求的url地址] 
 * @param {Object} params [请求时携带的参数] 
 */
export function postRequest(url, params) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseURL + url,
            data: params,
            header: {
              'Accept': 'application/json',
              'Content-Type': 'application/json',
// 				      // 'cookie': cookie
            },
            method: "POST",
            success: (response) => {
                let res = response.data
								resolve(res)
								// console.log(res);
        //         if (res.code === 200) {
        //             resolve(res)
        //         } else {
        //             reject(res)
        //             showError(res);
        //         }
            },
            fail: (error) => {
                if (error && error.response) {
                    reject(error.response)
                    showError(error.response);
                }
            },
        });
    })
}

/**
 * get方法，对应get请求
 * @param {String} url [请求的url地址]
 * @param {Object} params [请求时携带的参数]
 */
export function getRequest(url, params) {
    return new Promise((resolve, reject) => {
        uni.request({
            url: baseURL + url,
            data: params,
            header: {
                'Accept': 'application/json',
                'Content-Type': 'application/x-www-form-urlencoded', //自定义请求头信息
                // 'cookie': cookie
            },
            method: "GET",
            success: (response) => {
                let res = response.data
                if (res.code === 200) {
                    resolve(res)
                } else {
                    reject(res)
                    showError(res);
                }
            },
            fail: (error) => {
                if (error && error.response) {
                    reject(error.response)
                    showError(error.response);
                }
            }
        });
    })
}

const showError = error => {
    let errorMsg = ''
    switch (error.code) {
     	case 301:
            errorMsg = '未授权，请登录'
            break
        case 400:
            errorMsg = '请求参数错误'
            break 
        case 403:
            errorMsg = '跨域拒绝访问'
            break
        case 404:
            errorMsg = '请求地址不存在'
            break
        case 500:
            errorMsg = '服务器内部错误'
            break
        case 504:
            errorMsg = '请求超时'
            break
        default:
            errorMsg = "请求失败"
            break
    }
		uni.showToast({
			title: errorMsg,
			icon:'error',
			duration: 2000
		});
		// return Promise.reject(error);
}